package com.yks.rpc;

import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GreetingServer3 {
    public static void main(String[] args) throws Exception {
        Server server = new Server(3000);

        //servlet接受请求
        ServletContextHandler ctx = new ServletContextHandler();

        ServletHolder holder = new ServletHolder(new RequestServlet());
        ctx.addServlet(holder,"/*");
        server.setHandler(ctx);

        server.start();
        server.join();
    }
    static class RequestServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletOutputStream outputStream = resp.getOutputStream();
            byte[] bytes = IOUtils.readFully(req.getInputStream(), req.getInputStream().available());
            String string = new String(bytes);
            System.out.println(string);
            outputStream.write("server:hello".getBytes());
            outputStream.flush();
        }
    }
}
